const puppeteer = require('puppeteer');
const FakeUseragent = require('fake-useragent');

(async () => {
    const browser = await puppeteer.launch({
        headless: true
    })

    // 搜索关键字
    let searchText = '华农'
    let pageNo = 2

    const page = await browser.newPage()
    page.setUserAgent(FakeUseragent())
    await page.goto(`https://search.bilibili.com/all?keyword=${searchText}&from_source=webtop_search&spm_id_from=333.851&page=${pageNo}`)
    await page.setCookie({
        name: 'buvid3',
        value: 'E9C8C0C3-E03E-4744-83BA-9B7DFF680763155814infoc'
    })
    await page.waitForSelector('#all-list > div.flow-loader > div.mixin-list > ul')

    // 获取最大页码
    let maxPageNo = +(await page.$$eval('#all-list > div.flow-loader > div.page-wrap > div > ul > li.page-item.last > button', el => {
        return el.map(v => v.textContent)
    }))[0].trim()

    let resList = await page.$$eval('#all-list > div.flow-loader > div.mixin-list > ul > li > a', el => {
        return el.map(v => ({
            title: v.title,
            playUrl: v.href
        }))
    })

    for (pageNo = 2; pageNo <= 2; pageNo++) {
        page.setUserAgent(FakeUseragent())
        await page.goto(`https://search.bilibili.com/all?keyword=${searchText}&from_source=webtop_search&spm_id_from=333.851&page=${pageNo}`)
        // console.log(page.url())
        await page.waitForSelector('#all-list > div.flow-loader > ul', {
            timeout: 1000
        }).then(async res => {
            let item = await page.$$eval('#all-list > div.flow-loader > div.mixin-list > ul > li > a', el => {
                return el.map(v => ({
                    title: v.title,
                    playUrl: v.href
                }))
            })
            console.log(item)
            resList.push(...item)
            console.log(pageNo)
        }).catch(err => {
            console.log(`page:${pageNo} error!!!`)
        })
    }

    console.log(resList.length)
})()